package com.eeepay.eeepay_v2.activity;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.model.AgentAddInfo;
import com.eeepay.eeepay_v2.util.AllUtils;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.SPUtils;
import com.eeepay.eeepay_v2.view.AreaSelectView;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.util.ABAppUtil;
import com.eeepay.v2_library.util.ABRegUtil;
import com.eeepay.v2_library.view.LabelEditText;
import com.eeepay.v2_library.view.LeftRightText;

/**
 * Created by Ching on 2017/2/23.
 * 新增代理商第一步 --- 基本资料
 */

public class AgentAddStep1 extends ABBaseActivity implements View.OnClickListener {

    private int[] baseInfoIds = {R.id.et_merchant_name, R.id.et_phone, R.id.et_email, R.id.et_people_name,
            R.id.et_area, R.id.et_sale_name};

    private LabelEditText[] et_baseInfos;//基本信息所有项
    private LeftRightText addressLrt;
    private AreaSelectView areaPopView;
    private Button btn_confirm;

    private AgentAddInfo addInfo;
    private String province, city, area;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_agent_add_step_1;
    }

    @Override
    protected void initWidget() {
        btn_confirm = getViewById(R.id.btn_next);

        et_baseInfos = new LabelEditText[baseInfoIds.length];
        for (int i = 0; i < et_baseInfos.length; i++) {
            et_baseInfos[i] = getViewById(baseInfoIds[i]);
        }
        et_baseInfos[0].setFilter(10);
        et_baseInfos[1].setFilter(11);
        et_baseInfos[3].setFilter(10);
        et_baseInfos[5].setFilter(10);
        addressLrt = getViewById(R.id.lrt_address);

        areaPopView = new AreaSelectView(mContext);
        areaPopView.setOnAreaSelectedListener(new AreaSelectView.OnAreaSelectedListener() {
            @Override
            public void onSelected(String address) {
                addressLrt.setRightText(address);
                addressLrt.setRightTextColor(R.color.titlebar_title_txt_color);
                province = areaPopView.getProvinceSelected();
                area = areaPopView.getCounySelected();
                city = areaPopView.getCitySelected().substring(0, areaPopView.getCitySelected().length());

                areaPopView.dismiss();
            }
        });
        SPUtils.removeHistory(Constant.KEY.AGENT_ADD_INFO);

//        /** 模拟 **/
//        et_baseInfos[0].setEditContent("商通贷");
//        et_baseInfos[1].setEditContent("18300070842");0
//        et_baseInfos[2].setEditContent("1029283474@qq.com");
//        et_baseInfos[3].setEditContent("陈奕涵");
//        addressLrt.setRightText("广东-深圳市-南山区");
//        province = "广东";
//        city = "深圳市";
//        area = "南山区";
//        et_baseInfos[4].setEditContent("朗山路");
//        et_baseInfos[5].setEditContent("渣渣");

    }

    @Override
    protected void eventOnClick() {
        btn_confirm.setOnClickListener(this);
        addressLrt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_next:
                for (LabelEditText editText : et_baseInfos) {
                    if (TextUtils.isEmpty(editText.getEditContent())) {
                        showToast(" "+editText.getLabel().replace(":", "") + " 未填写，请填写完整");
                        return;
                    }
                }
                /** 名称 **/
                if (!ABRegUtil.isRegiest(et_baseInfos[0].getEditContent(), Constant.MER_NAME_REGEX)
                        || ABRegUtil.isRegiest(et_baseInfos[0].getEditContent(), Constant.NUM_LET_REGEX)) {
                    showToast("请填写有效的名称");
                    return;
                }
                /** 手机号 **/
                if (!ABRegUtil.isRegiest(et_baseInfos[1].getEditContent(), ABRegUtil.REG_PHONE_CHINA)) {
                    showToast("请输入正确手机号");
                    return;
                }
                /** 邮箱 **/
                if (!ABRegUtil.isRegiest(et_baseInfos[2].getEditContent(), ABRegUtil.REG_EMAIL)) {
                    showToast("请输入正确邮箱");
                    return;
                }
                /** 联系人 **/
                if (!ABRegUtil.isRegiest(et_baseInfos[3].getEditContent(), Constant.NAME_REGEX)
                        || ABRegUtil.isRegiest(et_baseInfos[3].getEditContent(), Constant.NUM_LET_REGEX)) {
                    showToast("请填写有效的联系人");
                    return;
                }
                /** 代理商省市区 **/
                if ("省/市/区".equals(addressLrt.getRighText()) || TextUtils.isEmpty(addressLrt.getRighText())) {
                    showToast("请选择代理商地址");
                    return;
                }
                /** 销售名称 **/
                if (!ABRegUtil.isRegiest(et_baseInfos[5].getEditContent(), Constant.MER_NAME_REGEX)
                        || ABRegUtil.isRegiest(et_baseInfos[5].getEditContent(), Constant.NUM_LET_REGEX)) {
                    showToast("请填写有效的销售名称");
                    return;
                }

                addInfo = SPUtils.getAgentAddInfo();
                if (addInfo == null)
                    addInfo = new AgentAddInfo();

                addInfo.setAgentName(et_baseInfos[0].getEditContent());
                addInfo.setPhone(et_baseInfos[1].getEditContent());
                addInfo.setEmail(et_baseInfos[2].getEditContent());
                addInfo.setUserName(et_baseInfos[3].getEditContent());
                addInfo.setProvince(province);
                addInfo.setCity(city);
                addInfo.setArea(area);
                addInfo.setAddress(et_baseInfos[4].getEditContent());
                addInfo.setSaleName(et_baseInfos[5].getEditContent());

                SPUtils.saveAgentAddInfo(addInfo);
                AllUtils.addActivity(AgentAddStep1.this);
                goActivity(AgentAddStep2.class);
                break;
            case R.id.lrt_address:
                ABAppUtil.hideSoftInput(this);
                if (areaPopView == null) {
                    areaPopView = new AreaSelectView(mContext);
                }
                areaPopView.showAtLocation(addressLrt, Gravity.BOTTOM, 0, 0);
                break;
        }
    }
}
